<?php

namespace App\Http\Controllers\Admin;

use App\Jobs\QuestionDeleteHelperJob;
use App\Models\Qh_Question;
use App\Models\QuestionReport;
use DB;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class QuestionReportController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|object
     */
    public function index()
    {
        $input = request()->all();
        $input['show'] = \request('show', 1);
        $list = QuestionReport::filter($input)
            ->select([
                'tiku_question_report.*',
                'user.nickname as userNickname',
                'user.avatar as userAvatar',
                'qh_questions.title as questionTitle',
            ])
            ->has('question')
            ->join('user', 'tiku_question_report.userId', '=', 'user.id')
            ->join('qh_questions', 'tiku_question_report.questionId', '=', 'qh_questions.id')
            // ->with(['user' => function($q) {
            //     $q->select(['id', 'avatar', 'username', 'nickname']);
            // }, 'question' => function($q) {
            //     $q->select(['id', 'title']);
            // }])
            ->orderByDesc('id')
            ->paginate(request('perPage', 10));

        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse|object
     * @throws \Throwable
     */
    public function destroy(int $id)
    {
        try {
            DB::transaction(function () use ($id) {
                $report = QuestionReport::findOrFail($id);

                $question = Qh_Question::find($report->questionId);
                if ($question) {
                    // 从用户的刷题记录、错题集、收藏集中删除该条数据
                    QuestionDeleteHelperJob::dispatch($question);
                }
                QuestionReport::where('id', $id)->delete();
            });
        }catch (\Exception $e) {
            throw new \Exception('删除失败');
        }

        return $this->restful();
    }


    public function hide($id) {
        QuestionReport::where('id', $id)->update(['show' => 0]);

        return $this->restful();
    }

    /**
     * 批量删除
     * @return JsonResponse|object
     * @throws \Throwable
     */
    public function batchDelete() {
        $params = \request()->validate([
            'ids' => 'bail|required|array',
            'ids.*' => 'bail|required|int'
        ], \request()->all());

        try {
            DB::transaction(function () use ($params) {
                $questionIds = QuestionReport::whereIn('id', $params['ids'])->pluck('questionId')->toArray();

                $questions = Qh_Question::findMany($questionIds);
                foreach ($questions as $question) {
                    // 从用户的刷题记录、错题集、收藏集中删除该条数据
                    QuestionDeleteHelperJob::dispatch($question);
                }
                QuestionReport::whereIn('id', $params['ids'])->delete();
            });
        }catch (\Exception $e) {
            throw new \Exception('删除失败');
        }
        return $this->restful();
    }
}
